package com.farmpost.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmpost.server.mapper.BooksMapper;
import com.farmpost.server.pojo.Books;
import com.farmpost.server.pojo.RespBean;
import com.farmpost.server.service.IBooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.util.List;

/**
 * <p>
 * 书籍表 服务实现类
 * </p>
 *
 * @author king
 * @since 2022-04-08
 */
@Service
public class BooksServiceImpl extends ServiceImpl<BooksMapper, Books> implements IBooksService {

    @Autowired
    private BooksMapper booksMapper;

    /**
     * 买卖书籍信息
     *
     * @param books
     * @return
     */
    @Override
    public RespBean insertBooks(Books books) {
        int insert = booksMapper.insert(books);
        if (insert == 1)
            return RespBean.success("发布书籍信息成功");
        return RespBean.error("发布书籍信息失败");
    }

    /**
     * 查询售卖书籍信息
     *
     * @return
     */
    @Override
    public List<Books> getBooksBySell() {
        List<Books> sell = booksMapper.selectList(new QueryWrapper<Books>().eq("sell_or_buy", 0));
        return setFilds(sell);
    }

    /**
     * 查询购买书籍信息
     *
     * @return
     */
    @Override
    public List<Books> getBooksByBuy() {
        List<Books> buy = booksMapper.selectList(new QueryWrapper<Books>().eq("sell_or_buy", 1));
        return setFilds(buy);
    }

    /**
     * 修改书籍信息
     *
     * @param books
     * @return
     */
    @Override
    public RespBean updateBooks(Books books) {
        int update = booksMapper.update(books, new UpdateWrapper<Books>().eq("id", books.getId()));
        if (update == 1)
            return RespBean.success("书籍信息修改成功");
        return RespBean.error("书籍信息修改失败");
    }

    /**
     * 根据id删除书籍信息
     * @param id
     * @return
     */
    @Override
    public RespBean deleteBooks(Integer id) {
        int delete = booksMapper.delete(new QueryWrapper<Books>().eq("id", id));//底层调用的是逻辑删除（更新）的方法
        if (delete == 1)
            return RespBean.success("删除书籍信息成功");
        return RespBean.error("删除书籍信息成功");
    }

    /**
     * 查询全部书籍信息
     * @param bookPage
     * @return
     */
    @Override
    public List<Books> getBooks(Page<Books> bookPage) {
        QueryWrapper<Books> queryWrapper = new QueryWrapper<>();
        Page<Books> booksPage = booksMapper.selectPage(bookPage, queryWrapper);
        return setFilds(booksPage.getRecords());
    }

    private List<Books> setFilds(List<Books> list) {
        for (Books books : list) {
            if (books.getSellOrBuy())
                books.setDescSellBuy("买书");
            else
                books.setDescSellBuy("卖书");
        }
        return list;
    }
}
